package com.itheima.info.manage.sysytem.controller;

import com.itheima.info.manage.sysytem.domain.Student;
import com.itheima.info.manage.sysytem.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService StudentService = new StudentService();

    public void start() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到学生管理系统---------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查看学生");
            System.out.println("5:退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findallStudent();
                    break;
                case "5":
                    System.out.println("返回主界面");
                    return;
            }
        }
    }

    private void updateStudent() {
        Scanner sc = new Scanner(System.in);
        String sid;
        Student[] stu = StudentService.findallStudent();
        if (stu == null) {
            System.out.println("目前没有学生请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入您要修改的学号");
            sid = sc.next();
            boolean exists = StudentService.isExists(sid);
            if (!exists) {
                System.out.println("没有学号，请先添加");
            } else {
                break;
            }
        }
        System.out.println("请输入新的学生姓名");
        String newname = sc.next();
        System.out.println("请输入新的学生年龄");
        int newage = sc.nextInt();
        System.out.println("请输入新的学生生日");
        String newbirthday = sc.next();
        Student newstu=new Student(sid,newname,newage,newbirthday);

        StudentService.updateStudent(sid,newstu);
        System.out.println("修改成功");
    }

    private void deleteStudent() {
        Scanner sc = new Scanner(System.in);
        String sid;
        Student[] stu = StudentService.findallStudent();
        if (stu == null) {
            System.out.println("目前没有学生请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入您要删除的学号");
            sid = sc.next();
            boolean exists = StudentService.isExists(sid);
            if (!exists) {
                System.out.println("没有学号，请先添加");
            } else {
                break;
            }
        }
        StudentService.deleteStudent(sid);
        System.out.println("删除成功");

    }

    private void findallStudent() {
        Student[] stu = StudentService.findallStudent();
        if (stu == null) {
            System.out.println("目前没有学生请先添加");
        } else {
            System.out.println("学号\t姓名\t年龄\t生日");
            for (int i = 0; i < stu.length; i++) {
                Student student = stu[i];
                if (student != null) {
                    System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getBirthday());
                }

            }
        }
    }

    public void addStudent() {
        Scanner sc = new Scanner(System.in);
        String sid;
        //录入学生信息
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            boolean exists = StudentService.isExists(sid);
            if (exists) {
                System.out.println("已有学号，添加失败");
            } else {
                break;
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //封装学生
        Student stu = new Student(sid, name, age, birthday);
        //将学生对象，传递给StudentService中的addStudent方法
        boolean flag = StudentService.addStudent(stu);
        //根据Boolean的结果，在控制台打印成功/失败
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
